<template>
  <div
    style="
      height: 100vh;
      display: flex;
      flex-direction: column;
      justify-content: center;
      align-items: center;
      margin-bottom: -40px;
    "
  >
    <!-- <van-image src="./logo.webp"></van-image> -->
    <van-image
      :src="logo"
      width="100px"
      height="100px"
      radius="50%"
      style="width: 100px; height: 100px; border-radius: 50%"
    ></van-image>
    <van-form @submit="onSubmit">
      <van-cell-group inset>
        <van-field
          v-model="username"
          name="username"
          label="用户名"
          placeholder="用户名"
          :rules="[{ required: true, message: '请填写用户名' }]"
        />
        <van-field
          v-model="password"
          type="password"
          name="password"
          label="密码"
          placeholder="密码"
          :rules="[{ required: true, message: '请填写密码' }]"
        />
      </van-cell-group>
      <div style="margin: 16px">
        <van-button round block type="primary" native-type="submit">
          登录
        </van-button>
      </div>
    </van-form>
  </div>
</template>

<script setup lang="ts">
import { onUnmounted, ref } from 'vue'
import { useBottomStore } from '@/stores/buttom'
// import logo from '@/assets/image/logo.webp'
import logo from '@/assets/image/grid/logo.webp'

import { showToast } from 'vant'
import req from '@/utils/req'
import { useRoute, useRouter } from 'vue-router'
const store = useBottomStore()
const router = useRouter()
const route = useRoute()
// store.changeBottomStatus()
onUnmounted(() => {
  // store.changeBottomStatus()
})
const username = ref('')
const password = ref('')
const onSubmit = (values:any) => {
  const { username, password } = values
  req
    .post('http://localhost:5000/api/v1/login', { username, password })
    .then((res) => {
      if (res.data.code === 0) {
        localStorage.setItem('token', res.data.token)
        showToast({
          message: '登录成功',
          icon: 'like-o',
          onClose: () => {
            const goto: any = route.query.goto ? route.query.goto : '/my'
            router.push(goto)
          }
        })
      } else {
        showToast({
          message: '登录失败',
          type: 'fail',
          onClose: () => {
            window.location.reload()
          }
        })
      }
    })
  console.log('submit', values)
}
</script>

<style lang="scss" scoped></style>
